home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 22 / PC Actual CD 22.iso / progs / Netobj / netobj / t2.z / RotatingPicture.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-04-29  |  8.1 KB  |  238 lines

  1. import java.applet.Applet;
  2. import java.awt.Color;
  3. import java.awt.Component;
  4. import java.awt.Dimension;
  5. import java.awt.Event;
  6. import java.awt.Graphics;
  7. import java.awt.Image;
  8. import java.awt.MediaTracker;
  9. import java.awt.Rectangle;
  10. import java.awt.image.ImageObserver;
  11. import java.net.URL;
  12.  
  13. public class RotatingPicture extends Applet implements Runnable {
  14.    Thread m_RotatingPicture;
  15.    private Graphics m_Graphics;
  16.    private Image[] m_Images;
  17.    private int m_nCurrImage;
  18.    private int m_nImgWidth;
  19.    private int m_nImgHeight;
  20.    private boolean m_fAllLoaded;
  21.    private boolean NetworkFiles;
  22.    private boolean didClick;
  23.    private int pauseTime;
  24.    private int numImages;
  25.    private String[] imageNames;
  26.    private String[] imageURLs;
  27.    protected Color backgroundColor;
  28.    protected Image backgroundImage;
  29.    protected int xOffset;
  30.    protected int yOffset;
  31.  
  32.    public void start() {
  33.       if (this.m_RotatingPicture == null) {
  34.          this.m_RotatingPicture = new Thread(this);
  35.          this.m_RotatingPicture.start();
  36.       }
  37.  
  38.    }
  39.  
  40.    private int convertBGRtoRGB(int BGRColor) {
  41.       int r = (BGRColor & 255) << 16;
  42.       int g = BGRColor & '\uff00';
  43.       int b = (BGRColor & 16711680) >> 16;
  44.       return r + g + b;
  45.    }
  46.  
  47.    public void stop() {
  48.       if (this.m_RotatingPicture != null) {
  49.          this.m_RotatingPicture.stop();
  50.          this.m_RotatingPicture = null;
  51.       }
  52.  
  53.    }
  54.  
  55.    public boolean mouseExit(Event evt, int x, int y) {
  56.       this.didClick = false;
  57.       return true;
  58.    }
  59.  
  60.    private String modifyStringContext(String param) {
  61.       if (param.startsWith(".")) {
  62.          param = param.replace('\\', '/');
  63.          String fullBase = ((Applet)this).getDocumentBase().toString();
  64.          param = fullBase.substring(0, fullBase.lastIndexOf(47)) + "/" + param;
  65.       } else if (param.startsWith("#")) {
  66.          param = ((Applet)this).getDocumentBase().toString() + param;
  67.       } else if (!param.startsWith("cid:") && !param.startsWith("lifn:") && !param.startsWith("java:") && !param.startsWith("irc:") && !param.startsWith("IOR:") && !param.startsWith("ilu:") && !param.startsWith("https:") && !param.startsWith("http:") && !param.startsWith("hdl:") && !param.startsWith("gopher:") && !param.startsWith("ftp:") && !param.startsWith("finger:") && !param.startsWith("file:") && !param.startsWith("data:") && !param.startsWith("clsid:") && !param.startsWith("md5:") && !param.startsWith("mailserver:") && !param.startsWith("mailto:") && !param.startsWith("mid:") && !param.startsWith("news:") && !param.startsWith("nntp:") && !param.startsWith("path:") && !param.startsWith("prospero:") && !param.startsWith("service:") && !param.startsWith("shttp") && !param.startsWith("snews") && !param.startsWith("STANF:") && !param.startsWith("telnet:") && !param.startsWith("vemmi:") && !param.startsWith("wais:") && !param.startsWith("whois++:")) {
  68.          param = "http://" + param;
  69.       }
  70.  
  71.       return param;
  72.    }
  73.  
  74.    public boolean mouseDown(Event evt, int x, int y) {
  75.       this.didClick = true;
  76.       return true;
  77.    }
  78.  
  79.    public boolean mouseEnter(Event evt, int x, int y) {
  80.       return true;
  81.    }
  82.  
  83.    private void displayImage(Graphics g) {
  84.       if (this.m_fAllLoaded) {
  85.          if (this.backgroundImage != null) {
  86.             int imHeight = this.backgroundImage.getHeight(this);
  87.             int imWidth = this.backgroundImage.getWidth(this);
  88.             Dimension d = ((Component)this).size();
  89.             Image theBuffer = ((Component)this).createImage(d.width, d.height);
  90.             Graphics gr = theBuffer.getGraphics();
  91.             int x = (d.width + this.xOffset) / imWidth + 1;
  92.             int y = (d.height + this.yOffset) / imHeight + 1;
  93.  
  94.             for(int i = 0; i < y; ++i) {
  95.                for(int j = 0; j < x; ++j) {
  96.                   gr.drawImage(this.backgroundImage, j * imWidth - this.xOffset, i * imHeight - this.yOffset, this);
  97.                }
  98.             }
  99.  
  100.             gr.drawImage(this.m_Images[this.m_nCurrImage], (((Component)this).size().width - this.m_Images[this.m_nCurrImage].getWidth(this)) / 2, (((Component)this).size().height - this.m_Images[this.m_nCurrImage].getHeight(this)) / 2, (ImageObserver)null);
  101.             g.drawImage(theBuffer, 0, 0, this);
  102.          } else {
  103.             g.clearRect(0, 0, ((Component)this).size().width, ((Component)this).size().height);
  104.             g.drawImage(this.m_Images[this.m_nCurrImage], (((Component)this).size().width - this.m_Images[this.m_nCurrImage].getWidth(this)) / 2, (((Component)this).size().height - this.m_Images[this.m_nCurrImage].getHeight(this)) / 2, (ImageObserver)null);
  105.          }
  106.  
  107.       }
  108.    }
  109.  
  110.    public String getAppletInfo() {
  111.       return "Name: RotatingPicture\r\n" + "Author: Alex Kogon\r\n" + "";
  112.    }
  113.  
  114.    public boolean mouseUp(Event evt, int x, int y) {
  115.       if (this.didClick) {
  116.          try {
  117.             ((Applet)this).getAppletContext().showDocument(new URL(this.imageURLs[this.m_nCurrImage]));
  118.          } catch (Exception var6) {
  119.             ((Applet)this).getAppletContext().showStatus("Bad URL: " + this.imageURLs[this.m_nCurrImage]);
  120.          }
  121.       }
  122.  
  123.       return true;
  124.    }
  125.  
  126.    public void run() {
  127.       this.m_nCurrImage = 0;
  128.       if (!this.m_fAllLoaded) {
  129.          ((Component)this).repaint();
  130.          this.m_Graphics = ((Component)this).getGraphics();
  131.          this.m_Images = new Image[this.numImages];
  132.          MediaTracker tracker = new MediaTracker(this);
  133.  
  134.          for(int i = 1; i <= this.numImages; ++i) {
  135.             this.m_Images[i - 1] = ((Applet)this).getImage(((Applet)this).getDocumentBase(), this.imageNames[i - 1]);
  136.             tracker.addImage(this.m_Images[i - 1], 0);
  137.          }
  138.  
  139.          try {
  140.             tracker.waitForAll();
  141.             this.m_fAllLoaded = !tracker.isErrorAny();
  142.          } catch (InterruptedException var4) {
  143.          }
  144.  
  145.          if (!this.m_fAllLoaded) {
  146.             this.stop();
  147.             this.m_Graphics.drawString("Error loading images!", 10, 40);
  148.             return;
  149.          }
  150.       }
  151.  
  152.       ((Component)this).repaint();
  153.  
  154.       while(true) {
  155.          try {
  156.             this.displayImage(this.m_Graphics);
  157.             Thread.sleep((long)this.pauseTime);
  158.             ++this.m_nCurrImage;
  159.             if (this.m_nCurrImage == this.numImages) {
  160.                this.m_nCurrImage = 0;
  161.             }
  162.          } catch (Exception var5) {
  163.             this.stop();
  164.          }
  165.       }
  166.    }
  167.  
  168.    public void destroy() {
  169.    }
  170.  
  171.    public void init() {
  172.       this.xOffset = Integer.parseInt(((Applet)this).getParameter("X Position"));
  173.       this.yOffset = Integer.parseInt(((Applet)this).getParameter("Y Position"));
  174.       this.didClick = false;
  175.       this.pauseTime = 1000 * Integer.parseInt(((Applet)this).getParameter("Pause Time"));
  176.       this.numImages = Integer.parseInt(((Applet)this).getParameter("Number of Images"));
  177.       this.imageNames = new String[this.numImages];
  178.       this.imageURLs = new String[this.numImages];
  179.  
  180.       for(int cnt = 0; cnt < this.numImages; ++cnt) {
  181.          this.imageNames[cnt] = ((Applet)this).getParameter("Image " + (cnt + 1));
  182.          if (this.imageNames[cnt] != null && this.imageNames[cnt].startsWith("file://///")) {
  183.             this.NetworkFiles = true;
  184.          }
  185.  
  186.          this.imageURLs[cnt] = ((Applet)this).getParameter("URL for Image " + (cnt + 1));
  187.          if (this.imageURLs[cnt] != null) {
  188.             if (this.imageURLs[cnt].startsWith(".")) {
  189.                String theBase = ((Applet)this).getDocumentBase().toExternalForm();
  190.                int lastSlash = theBase.lastIndexOf(47);
  191.                this.imageURLs[cnt] = theBase.substring(0, lastSlash + 1) + this.imageURLs[cnt];
  192.             } else if (!this.imageURLs[cnt].startsWith("cid:") && !this.imageURLs[cnt].startsWith("lifn:") && !this.imageURLs[cnt].startsWith("java:") && !this.imageURLs[cnt].startsWith("irc:") && !this.imageURLs[cnt].startsWith("IOR:") && !this.imageURLs[cnt].startsWith("ilu:") && !this.imageURLs[cnt].startsWith("https:") && !this.imageURLs[cnt].startsWith("http:") && !this.imageURLs[cnt].startsWith("hdl:") && !this.imageURLs[cnt].startsWith("gopher:") && !this.imageURLs[cnt].startsWith("ftp:") && !this.imageURLs[cnt].startsWith("finger:") && !this.imageURLs[cnt].startsWith("file:") && !this.imageURLs[cnt].startsWith("data:") && !this.imageURLs[cnt].startsWith("clsid:") && !this.imageURLs[cnt].startsWith("md5:") && !this.imageURLs[cnt].startsWith("mailserver:") && !this.imageURLs[cnt].startsWith("mailto:") && !this.imageURLs[cnt].startsWith("mid:") && !this.imageURLs[cnt].startsWith("news:") && !this.imageURLs[cnt].startsWith("nntp:") && !this.imageURLs[cnt].startsWith("path:") && !this.imageURLs[cnt].startsWith("prospero:") && !this.imageURLs[cnt].startsWith("service:") && !this.imageURLs[cnt].startsWith("shttp") && !this.imageURLs[cnt].startsWith("snews") && !this.imageURLs[cnt].startsWith("STANF:") && !this.imageURLs[cnt].startsWith("telnet:") && !this.imageURLs[cnt].startsWith("vemmi:") && !this.imageURLs[cnt].startsWith("wais:") && !this.imageURLs[cnt].startsWith("whois++:")) {
  193.                this.imageURLs[cnt] = "http://" + this.imageURLs[cnt];
  194.             }
  195.          }
  196.       }
  197.  
  198.       if (((Applet)this).getParameter("BackgroundColor") != null) {
  199.          int backgroundcolor = Integer.parseInt(((Applet)this).getParameter("BackgroundColor"));
  200.          backgroundcolor = this.convertBGRtoRGB(backgroundcolor);
  201.          this.backgroundColor = new Color(backgroundcolor);
  202.          ((Component)this).setBackground(this.backgroundColor);
  203.       } else if (((Applet)this).getParameter("BackgroundImage") != null) {
  204.          String backgroundimage = this.modifyStringContext(((Applet)this).getParameter("BackgroundImage"));
  205.  
  206.          try {
  207.             this.backgroundImage = ((Applet)this).getImage(new URL(backgroundimage));
  208.          } catch (Exception var6) {
  209.             System.out.println("Error forming URL for background image");
  210.             return;
  211.          }
  212.  
  213.          MediaTracker tracker = new MediaTracker(this);
  214.          tracker.addImage(this.backgroundImage, 0);
  215.  
  216.          try {
  217.             tracker.waitForID(0);
  218.          } catch (InterruptedException var5) {
  219.             System.out.println("Background image loading interrupted");
  220.          }
  221.       }
  222.  
  223.    }
  224.  
  225.    public void paint(Graphics g) {
  226.       if (this.m_fAllLoaded) {
  227.          Rectangle r = g.getClipRect();
  228.          g.clearRect(r.x, r.y, r.width, r.height);
  229.          this.displayImage(g);
  230.       } else if (this.NetworkFiles) {
  231.          g.drawString("Unable to load images over Windows network!", 10, 20);
  232.       } else {
  233.          g.drawString("Loading images...", 10, 20);
  234.       }
  235.  
  236.    }
  237. }
  238.